package com.maila.calltaxi.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.maila.calltaxi.R;
import com.maila.calltaxi.utils.NullCheckUtil;
import com.maila.calltaxi.utils.RegularUtils;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.title_login)
    TextView titleLogin;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.btn_getcode)
    TextView btnGetcode;
    @BindView(R.id.et_passwd)
    EditText etPasswd;
    @BindView(R.id.bt_login)
    Button btLogin;
    @BindView(R.id.mainlayout)
    AutoLinearLayout mainlayout;
    private int countdown = 60;
    Timer mTimer;
    boolean isSendSuccess = false;

    private Handler mHandler = new Handler() {

        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                isSendSuccess = false;
                btnGetcode.setEnabled(true);
                btnGetcode.setClickable(true);
                btnGetcode.setBackgroundResource(R.drawable.tv_bg_cz_gree);
                btnGetcode.setText("重新发送");
                if (mTimer != null) {
                    mTimer.cancel();
                    mTimer = null;
                }
            } else {
                btnGetcode.setEnabled(false);
                btnGetcode.setBackgroundResource(R.drawable.tv_bg_cz_hui);
                btnGetcode.setText(msg.what + "秒");
            }
        }
    };
    private boolean IsPhone=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(RegularUtils.isPhoneNumber(s.toString())){
                    IsPhone=true;
                }else {
                    IsPhone=false;
                }
            }
        });
    }

    private void startAmap() {
        Intent intent = new Intent(MainActivity.this, AmapActivity.class);
        startActivity(intent);
        overridePendingTransition(0, 0);
    }

    @OnClick({R.id.btn_getcode, R.id.bt_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_getcode:
                if(IsPhone){
                    if (!isSendSuccess) {
                        countdown = 60;
                        mTimer = new Timer();
                        mTimer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                if (countdown >= 0) {
                                    mHandler.sendEmptyMessage(countdown);
                                    countdown--;
                                }
                            }
                        }, 0, 1000);
                    }
                }else {
                    Toast.makeText(MainActivity.this,"请输入正确的手机号",Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.bt_login:
                if(NullCheckUtil.checkNullPoint(etPhone.getText().toString())&&NullCheckUtil.checkNullPoint(etPasswd.getText().toString())){
                    if(RegularUtils.isPhoneNumber(etPhone.getText().toString())){
                        startAmap();
                        finish();
                    }else {
                        Toast.makeText(this,"请输入正确的手机号",Toast.LENGTH_SHORT).show();
                    }

                }else {
                    Toast.makeText(this,"您没有输入完呢",Toast.LENGTH_SHORT).show();
                }

                break;
        }
    }
}
